package com.topxgun.gcode.service;

import com.alibaba.fastjson.JSONObject;
import com.topxgun.gcode.utils.FileUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

public class CodeUtils {

    /**
     * 根据输入信息生成实体类
     * @return
     */
    public static JSONObject entityCode(Map<String,Object> paramMap,String fltName,String outFileName,String outPath,String packageName){
        JSONObject result = new JSONObject();
        String packagePath[] = packageName.split("\\.");
        StringBuilder packageUrl = new StringBuilder();
        for (String item : packagePath){
            packageUrl.append("/").append(item).append("/");
        }
        packageUrl.append("entity/");
        try{
            Configuration configuration = new Configuration();
            Template template = configuration.getTemplate(fltName);
            FileUtils.fileExit(outPath+"/"+packageUrl);
            Writer writer  = new OutputStreamWriter(new FileOutputStream(outPath+"/"+packageUrl+outFileName+"Entity.java",false),"UTF-8");
            template.process(paramMap, writer);
            result.put("code","200");
            result.put("msg","成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code","100");
            result.put("msg","系统异常");
        }
        return  result;
    }

    public static JSONObject daoCode(Map<String,Object> paramMap,String fltName,String outFileName,String outPath,String packageName){
        JSONObject result = new JSONObject();
        String packagePath[] = packageName.split("\\.");
        StringBuilder packageUrl = new StringBuilder();
        for (String item : packagePath){
            packageUrl.append("/").append(item).append("/");
        }
        packageUrl.append("dao/");
        try{
            Configuration configuration = new Configuration();
            Template template = configuration.getTemplate(fltName);
            FileUtils.fileExit(outPath+"/"+packageUrl);
            Writer writer  = new OutputStreamWriter(new FileOutputStream(outPath+"/"+packageUrl+outFileName+"Dao.java",false),"UTF-8");
            template.process(paramMap, writer);
            result.put("code","200");
            result.put("msg","成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code","100");
            result.put("msg","系统异常");
        }
        return  result;
    }

    public static JSONObject daoImpCode(Map<String,Object> paramMap,String fltName,String outFileName,String outPath,String packageName){
        JSONObject result = new JSONObject();
        String packagePath[] = packageName.split("\\.");
        StringBuilder packageUrl = new StringBuilder();
        for (String item : packagePath){
            packageUrl.append("/").append(item).append("/");
        }
        packageUrl.append("dao/impl/");
        try{
            Configuration configuration = new Configuration();
            Template template = configuration.getTemplate(fltName);
            FileUtils.fileExit(outPath+"/"+packageUrl);
            Writer writer  = new OutputStreamWriter(new FileOutputStream(outPath+"/"+packageUrl+"/"+outFileName+"DaoImpl.java",false),"UTF-8");
            template.process(paramMap, writer);
            result.put("code","200");
            result.put("msg","成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code","100");
            result.put("msg","系统异常");
        }
        return  result;
    }

    public static JSONObject serviceCode(Map<String,Object> paramMap,String fltName,String outFileName,String outPath,String packageName){
        JSONObject result = new JSONObject();
        String packagePath[] = packageName.split("\\.");
        StringBuilder packageUrl = new StringBuilder();
        for (String item : packagePath){
            packageUrl.append("/").append(item).append("/");
        }
        packageUrl.append("service/");
        try{
            Configuration configuration = new Configuration();
            Template template = configuration.getTemplate(fltName);
            FileUtils.fileExit(outPath+"/"+packageUrl);
            Writer writer  = new OutputStreamWriter(new FileOutputStream(outPath+"/"+packageUrl+"/"+outFileName+"Service.java",false),"UTF-8");
            template.process(paramMap, writer);
            result.put("code","200");
            result.put("msg","成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code","100");
            result.put("msg","系统异常");
        }
        return  result;
    }

    public static JSONObject serviceImplCode(Map<String,Object> paramMap,String fltName,String outFileName,String outPath,String packageName){
        JSONObject result = new JSONObject();
        String packagePath[] = packageName.split("\\.");
        StringBuilder packageUrl = new StringBuilder();
        for (String item : packagePath){
            packageUrl.append("/").append(item).append("/");
        }
        packageUrl.append("service/impl/");
        try{
            Configuration configuration = new Configuration();
            Template template = configuration.getTemplate(fltName);
            FileUtils.fileExit(outPath+"/"+packageUrl);
            Writer writer  = new OutputStreamWriter(new FileOutputStream(outPath+"/"+packageUrl+"/"+outFileName+"ServiceImpl.java",false),"UTF-8");
            template.process(paramMap, writer);
            result.put("code","200");
            result.put("msg","成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code","100");
            result.put("msg","系统异常");
        }
        return  result;
    }

    public static JSONObject controllerCode(Map<String,Object> paramMap,String fltName,String outFileName,String outPath,String packageName){
        JSONObject result = new JSONObject();
        String packagePath[] = packageName.split("\\.");
        StringBuilder packageUrl = new StringBuilder();
        for (String item : packagePath){
            packageUrl.append("/").append(item).append("/");
        }
        try{
            Configuration configuration = new Configuration();
            Template template = configuration.getTemplate(fltName);
            FileUtils.fileExit(outPath+"/"+packageUrl);
            Writer writer  = new OutputStreamWriter(new FileOutputStream(outPath+"/"+packageUrl+"/"+outFileName+"Controller.java",false),"UTF-8");
            template.process(paramMap, writer);
            result.put("code","200");
            result.put("msg","成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code","100");
            result.put("msg","系统异常");
        }
        return  result;
    }
}
